package com.tokenplus.thai.launch

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.tokenplus.thai.ProjectConfig
import com.tokenplus.thai.R
import com.tokenplus.thai.aviewmodel.AppViewModel
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.components.dialog.AreaNotAllowedDialog
import com.tokenplus.thai.data.ReqBase
import com.tokenplus.thai.databinding.ActivityLaunchBinding
import com.tokenplus.thai.enums.PageTagEnum
import com.tokenplus.thai.extend.*
import com.tokenplus.thai.utils.UserStateUtil
import com.tokenplus.thai.utils.update.ZHIVersionIMPL
import java.util.*


/** 启动页 */
@SuppressLint("CustomSplashScreen")
class LaunchActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityLaunchBinding

    //app viewModel
    private val mAppViewModel by lazy {
        ViewModelProvider(this)[AppViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mAppViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.launch.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityLaunchBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetTextI18n")
    override fun onContentViewBinded() {
        try {
            //如果已打开，就退出
//            if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
//                finish()
//                return
//            }

//            UserStateUtil.logout()

            //监听live data 返回
            onHandleLiveDataCallback()

            //初始化版本信息
            mActivityBinding.textLaunchProductVersion.text = getString(
                R.string.version_flag,
                "${ProjectConfig.appVersionName}(${ProjectConfig.appVersion})"
            )

            //查询版本号
            // mAppViewModel.appVersion(ReqBase(showLoading = false))

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()

        // 拦截中文
        if (isChineseLanguage()) {
            AreaNotAllowedDialog.showDialog(supportFragmentManager)
        } else {
            // 获取ip信息
            mAppViewModel.getIpInfo(ReqBase())
        }
    }


    /** 监听live data 返回 */
    private fun onHandleLiveDataCallback() {
        mAppViewModel.onGetIpInfoLiveData.observe(this) {
            //查询版本号
            mAppViewModel.appVersion(ReqBase(showLoading = false))
        }

        mAppViewModel.onAppVersionLiveData.observe(this) {
            var needUpdate = false
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    //判断是否需要更新
                    needUpdate = ZHIVersionIMPL.isAppNeedUpdate(this, result)
                }
            }

            println("=======needUpdate: $needUpdate")

            if (!needUpdate) {
                //下一步页面跳转
                checkNextPage()
            }
        }
    }

    /** 下一步页面跳转 */
    private fun checkNextPage() {
        if (UserStateUtil.isDidLogin()) {
            startToHomeActivity()

        } else {
            startToLoginActivity()
        }
    }

    /** 判断语言是否为中文，要拦截 */
    fun isChineseLanguage(): Boolean {
        val currentLocale = Locale.getDefault()
        val language = currentLocale.language
        println("======>lang: $language")
        return language == "zh"
    }
}